define(function(require, exports, module) {

	"use strict";
	require("mui");
	//  require("../zepto.min.js");
	require("jquery");
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var newAdd = require("../chioceProduct/newAdd.js");
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//钉钉返回清除缓存
	var dingTalkNav = require('dd_config');
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});

	$(function() {
		//钉钉右边按钮操作
		dingTalkNav.RightNavBar(false, "新增", "default");
		mui("body").on("tap", "header>button", function() {
			clearSession();
		});

		function clearSession() {
			sessionStorage.removeItem("clientData");
			sessionStorage.removeItem("clientName");
			sessionStorage.removeItem("clientNameId");
			sessionStorage.removeItem("chioceStaff");
			sessionStorage.removeItem("chioceStaffId");
			sessionStorage.removeItem("clientData");
		}

		function clearSessionDDUpdate() {
			sessionStorage.removeItem("clientData");
			sessionStorage.removeItem("clientName");
			sessionStorage.removeItem("clientNameId");
			sessionStorage.removeItem("chioceStaff");
			sessionStorage.removeItem("chioceStaffId");
			sessionStorage.removeItem("clientData");
			document.location.href = "../../src/chioceClient/clientDetails.html?dd_nav_bgcolor=FFFF943E";
		}

		function clearSessionDDCrerate() {
			sessionStorage.removeItem("clientData");
			sessionStorage.removeItem("clientName");
			sessionStorage.removeItem("clientNameId");
			sessionStorage.removeItem("chioceStaff");
			sessionStorage.removeItem("chioceStaffId");
			sessionStorage.removeItem("clientData");
			document.location.href = "../../src/chioceClient/chioceClientList.html?dd_nav_bgcolor=FFFF943E";
		}
		//返回
		if(sessionStorage.getItem("clientData")) {
			//ios
			dingTalkNav.LeftNavBar(clearSessionDDUpdate);
			//ad
			dingTalkNav.leftAndroid(clearSessionDDUpdate);
			//update
			dingTalkNav.setTitleText("编辑客户");
		} else {
			//ios
			dingTalkNav.LeftNavBar(clearSessionDDCrerate);
			//ad
			dingTalkNav.leftAndroid(clearSessionDDCrerate);
			//create
			dingTalkNav.setTitleText("新增客户");
		}

		//禁止编辑
		$("#companySerial").addClass('mui-disabled');

		//编辑客户资料
		var data = sessionStorage.getItem("clientData");
		if(!data) {
			//客户编号
			var optionSource = {};
			var source = {
				"sourceType": abp.sourceType.INFO_CUSTOMER
			};
			abp.services.app.sysClientSettingNumber.getSerialNo(source, optionSource)
				.done(function(data) {
					//console.log(data.serialNo);
					$("#code").text(data.serialNo);
					if(data.success) {
						//console.log(data);
					} else {}
				})
				.fail(function(data) {

				});
		}

		if(data && data != "undefined") {
			//点击选择客户类型
			mui("body").on("tap", "#client-type", function() {
				//编辑之后再存储
				updateData();
			});
			//点击选择负责人
			mui("body").on("tap", "#staff-Client", function() {

				updateData();
				//dataModel需要传的模型值,option非必要参数，默认传空对象，done异步完成，fail异步失败，data.success数值成功返回 
			});
			data = JSON.parse(data);
			//			var titles = sessionStorage.getItem("title");
			//			var clientName = data.infoCompanyCatalog.companyCatalogName;
			var clientId = data.id;
			//			console.log(clientId);
			$("#code").text(data.companySerial); //客户编号
			$("#code").attr("data-id", data.id); //客户Id
			$("#companyName").val(data.companyName); //名称
			$("#clientStaff").html(data.salesMan); //负责人
			$("#companyCatalog").html(data.infoCompanyCatalog.companyCatalogName); //分类名称							
			$("#companyCatalog").attr("data-id", data.infoCompanyCatalog.id); //分类ID
			$("#amount").val(data.amount); //期初应付款
			$("#contact").val(data.infoContactList[0].contactName);
			$("#contact").attr("data-id", data.infoContactList[0].id); //联系人
			$("#contactMobile").val(data.infoContactList[0].contactMobile); //联系人手机
			$("#contactTel").val(data.infoContactList[0].contactTel); //联系人电话
			$("#address").val(data.address); //联系人地址
			$("#notes").val(data.notes); //备注信息
			newAdd.typeAndUser(".userName", "#clientStaff");
		}

		//传入数据
		function dataModels() {
			//获取值
			var companySerial = $("#code").text().trim();
			var companyName = $("#companyName").val().trim();
			var salesMan = $("#clientStaff").html().trim();
			var companyCatalogName = $("#companyCatalog").html().trim();
			var companyCatalogNameId = Number($("#companyCatalog").attr("data-id"));
			var amount = Number($("#amount").val().trim());
			var contact = $("#contact").val().trim();
			var contactMobile = $("#contactMobile").val().trim();
			var contactTel = $("#contactTel").val().trim();
			var address = $("#address").val().trim();
			var notes = $("#notes").val().trim();
			var ids = 0 || $("#code").attr("data-id");
			var contactId = 0 || $("#contact").attr("data-id");

			var modelData = {
				"infoCompanyCatalogId": companyCatalogNameId,
				"areaId": 0,
				"companySerial": companySerial,
				"companyName": companyName,
				"salesMan": salesMan,
				"invoiceName": "",
				"webSite": "",
				"address": address,
				"businessLicense": "",
				"taxCertNo": "",
				"bankName": "",
				"bankAccount": "",
				"receiptTypeDesc": "",
				"paymentTypeDesc": "",
				"notes": notes,
				"amount": amount,
				"statusId": abp.infoStatusId.ENABLE_STATUS,
				"infoContactList": [{
					"companyId": 0,
					"contactName": contact,
					"contactPost": "",
					"contactTel": contactTel,
					"contactMobile": contactMobile,
					"contactEmail": "",
					"contactFax": "",
					"contactAddress": "",
					"notes": "",
					"id": contactId
				}],
				"id": ids
			};

			return modelData;
		}
		newAdd.typeAndUser(".userName", "#clientStaff");
		//新增
		function newAddClient(dataModel) {
			var option = {};
			abp.services.app.customer.create(dataModel, option).
			done(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', "保存成功", 1, 3, clearData);

				function clearData() {
					sessionStorage.removeItem("clientName");
					sessionStorage.removeItem("clientNameId");
					sessionStorage.removeItem("chioceStaff");
					sessionStorage.removeItem("chioceStaffId");
					document.location.href = "../../src/chioceClient/chioceClientList.html?dd_nav_bgcolor=FFFF943E";
				}
				if(data.success) {

				} else {}
			}).fail(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', data.message, 2, 3, "default");
			});
		}
		//
		//修改
		function updateClient(dataModel) {
			var option = {};
			abp.services.app.customer.update(dataModel, option).
			done(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', "修改成功", 1, 3, clearUpdateData);

				function clearUpdateData() {
					sessionStorage.removeItem("clientName");
					sessionStorage.removeItem("clientNameId");
					sessionStorage.removeItem("chioceStaff");
					sessionStorage.removeItem("chioceStaffId");
					sessionStorage.removeItem("chioceClientName");
					sessionStorage.removeItem("chioceClientNameId");
					sessionStorage.removeItem("clientData");
					document.location.href = "../../src/chioceClient/clientDetails.html?dd_nav_bgcolor=FFFF943E";
				}
				if(data.success) {

				} else {}
			}).fail(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', data.message, 2, 3, "default");
			});
		}
		//		$("input").on("focus",function(){
		//			$(this).attr("placeholder","");
		//		});
		//修改供应商之后重新存储数据
		function updateData() {
			data.companySerial = $("#code").text();
			data.companyName = $("#companyName").val();
			data.infoCompanyCatalog.companyCatalogName = $("#companyCatalog").html();
			data.infoCompanyCatalog.id = $("#companyCatalog").attr("data-id");
			data.amount = $("#amount").val();
			data.infoContactList[0].contactName = $("#contact").val();
			data.infoContactList[0].id = $("#contact").attr("data-id"); //联系人
			data.infoContactList[0].contactMobile = $("#contactMobile").val(); //联系人手机
			data.infoContactList[0].contactTel = $("#contactTel").val(); //联系人电话
			data.address = $("#address").val(); //联系人地址
			data.notes = $("#notes").val(); //备注信息
			sessionStorage.clientData = JSON.stringify(data);
			//			console.log(data.companySerial);
		}

		var alertMore = require("../chioceClient/alertMore.js");
		var datamode = function() {
			var dataModel = dataModels();
			if(!dataModel.id || dataModel.id === undefined) {
				newAddClient(dataModel);
			} else {
				updateClient(dataModel);
			}
		};
		mui("body").on("tap", "#submit", function() {

			$("input").blur();
			$("textarea").blur();
			alertMore.alerTs(datamode);
			//dataModel需要传的模型值,option非必要参数，默认传空对象，done异步完成，fail异步失败，data.success数值成功返回 
		});

	});
});